package com.rttmall.shopbackend.app.gambling.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.rttmall.shopbackend.app.gambling.mapper.ProductCustomMapper;
import com.rttmall.shopbackend.app.gambling.mapper.VirtualDataCustomMapper;
import com.rttmall.shopbackend.app.gambling.mapper.VirtualDataMapper;
import com.rttmall.shopbackend.app.gambling.pojo.VirtualData;
import com.rttmall.shopbackend.app.gambling.pojo.VirtualDataCustom;
import com.rttmall.shopbackend.app.gambling.service.VritualDataService;
import com.rttmall.shopbackend.enums.VirtualStatus;
import com.rttmall.shopbackend.utils.ReadExcel;
@SuppressWarnings("all")
@Service
public class VritualDataServiceImpl implements VritualDataService {

	@Autowired
	private VirtualDataCustomMapper virtualDataCustomMapper;
	@Autowired
	private VirtualDataMapper virtualDataMapper;
	@Autowired
	private ProductCustomMapper productCustomMapper;
	
	@Override
	public Map queryVirtualData(VirtualDataCustom virtualDataCustom) {
		Map map = new HashMap();
        List<VirtualDataCustom> virtualDataCustomList = virtualDataCustomMapper.queryVirtualData(virtualDataCustom);
        int sum = virtualDataCustomMapper.queryVirtualDataCount(virtualDataCustom);
        map.put("data", virtualDataCustomList);
        map.put("total", sum);
        return map;
	}
	@Override
	public VirtualDataCustom editQueryVirtualData(Integer id) {
		VirtualDataCustom virtualDataCustom = virtualDataCustomMapper.queryVirtualDataById(id);
		return virtualDataCustom;
	}
	@Override
	public void insertVirtualDataSubmit(VirtualData virtualData) {
		virtualDataMapper.insertSelective(virtualData);
	}
	@Override
	public void updateVirtualDataSubmit(VirtualData virtualData) {
		virtualDataMapper.updateByPrimaryKeySelective(virtualData);
		
	}
	@Override
	public void deleteVirtualData(Integer id) {
		virtualDataMapper.deleteByPrimaryKey(id);
	}
	@Override
	public int batchImportVirtualData(String name, CommonsMultipartFile file) {
		ReadExcel readExcel = new ReadExcel();
		List<VirtualData> virtualDataList = readExcel.getExcelInfo(name,file);
		virtualDataList =  virtualDataList.stream().filter(w->{
			boolean flag = (!StringUtils.isEmpty(w.getProductId())&&!StringUtils.isEmpty(w.getVirtualKey())&&!StringUtils.isEmpty(w.getSecret()));
			return flag;
		}).collect(Collectors.toList());
		virtualDataList.forEach(x->{x.setStatus(VirtualStatus.NOT_GRANT.ordinal());});
		//判断该商品是否存在，是否是虚拟商品
		List<Integer> productIds = productCustomMapper.queryProductByIds(virtualDataList.stream().map(x->x.getProductId()).collect(Collectors.toList()));
		virtualDataList = virtualDataList.stream().filter(w->{
			return productIds.stream().anyMatch(x->x.equals(w.getProductId()));
		}).collect(Collectors.toList());
		//开始批量插入
		virtualDataCustomMapper.batchInsertVirtualData(virtualDataList);
		return virtualDataList.size();
	}
}
